import server from "@/server/index";
import Store from '@/store'
import Vue from "vue";
let userInfo = {
    uid: "",
    ottId: ""
};
Vue.Native.callNative('AtlasModule', 'dataGet', 'WK_USERINFO', v => {
    jsLog("callNative...userInfo..."+v)
    if (v) {
        userInfo = JSON.parse(v);
    }
})
const jsLog = (msg,msg1)=>{
    return
    if (typeof msg1 === "undefined") {
        console.log(msg)
    }else{
        console.log(msg,msg1)
    }
};
const formatDateAll = (date, format, isTimeShift) => {
    if (arguments.length < 2 && !date.getTime) {
        format = date;
        date = serverTime;
    }
    typeof format != "string" && (format = "YYYY年MM月DD日 hh时mm分ss秒");
    var week = [
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "日",
        "一",
        "二",
        "三",
        "四",
        "五",
        "六",
    ];
    var tmp;
    return format.replace(/YYYY|YY|MM|DD|hh|mm|ss|星期|周|www|week/g, function (
        a
    ) {
        switch (a) {
            case "YYYY":
                return date.getFullYear();
            case "YY":
                return (date.getFullYear() + "").slice(2);
            case "MM":
                tmp = date.getMonth() + 1 + "";
                if (isTimeShift) {
                    return tmp.length == 1 ? "0" + tmp : tmp;
                }
                return tmp.length == 1 && false ? "0" + tmp : tmp;
            case "DD":
                tmp = date.getDate() + "";
                if (isTimeShift) {
                    return tmp.length == 1 ? "0" + tmp : tmp;
                }
                return tmp.length == 1 && false ? "0" + tmp : tmp;
            case "hh":
                tmp = date.getHours() + "";
                return tmp.length == 1 ? "0" + tmp : tmp;
            case "mm":
                tmp = date.getMinutes() + "";
                return tmp.length == 1 ? "0" + tmp : tmp;
            case "ss":
                tmp = date.getSeconds() + "";
                return tmp.length == 1 ? "0" + tmp : tmp;
            case "星期":
                return "星期" + week[date.getDay() + 7];
            case "周":
                return "周" + week[date.getDay() + 7];
            case "week":
                return week[date.getDay()];
            case "www":
                return week[date.getDay()].slice(0, 3);
        }
    });
};
// 页面行为上报
const uploadLog = (eventId,content)=>{
    let _content = {};
    _content.utype = "0";
    _content.t = new Date().getTime();
    _content.event_id = eventId;
    _content.props = content;
    _content.v = 2;
    if (userInfo.uid === "" && userInfo.ottId === "" && !__IS_DEV__) {
        Vue.Native.callNative('AtlasModule', 'dataGet', 'WK_USERINFO', v => {
            jsLog("callNative...uploadLog...userInfo..."+v)
            if (v) {
                userInfo = JSON.parse(v);
                _content.uid = userInfo.uid;
                _content.ott_id = userInfo.ottId;
                _content = JSON.stringify(_content);
                sendLog(_content);
                _content = null;
            }
        })
    }else{
        _content.uid = userInfo.uid;
        _content.ott_id = userInfo.ottId;
        _content = JSON.stringify(_content);
        sendLog(_content);
        _content = null;
    }
}

// 调用400上报方法
const sendLog = (str)=>{
    jsLog("logger.info: sendLog...",str);
    server.logger(str).then(res=>{
        jsLog("logger.info: success...",res);
    }).catch(e=>{
        jsLog('logger.info error:' + e.message);
    })
}

export{
    jsLog,
    uploadLog,
    formatDateAll
}